package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.AdminMapper;
import com.bsh.mybatis.model.Admin;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Repository
public class DaoAdmin {
	@Resource
	SqlSessionTemplate session;
	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;

	AdminMapper getMapper() {
		return session.getMapper(AdminMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(Admin record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(Admin record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public Admin selectByPrimaryKey(Integer id) {
		Admin row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(Admin record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(Admin record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	/**
	 * 取得登录用户信息
	 */
	public Admin getLoginUser(String loginName, String password) {
		AdminMapper dao = session.getMapper(AdminMapper.class);
		Admin admin = dao.getLoginUser(loginName, password);
		return admin;
	}

	/**
	 * 管理员分页
	 */
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("SELECT * from admin ");
		sb.append("where login_name<>'admin' ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		sb.append(" order by id desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}

	/**
	 * 添加管理员
	 * 
	 * @param adminid
	 * @return
	 */
	public Integer addAdmin(Admin admin) {
		AdminMapper map = this.getMapper();
		return map.insert(admin);
	}

	/**
	 * 修改管理员
	 * 
	 * @param adminid
	 * @return
	 */
	public Integer editAdmin(Admin admin) {
		AdminMapper map = this.getMapper();
		return map.editAdmin(admin);
	}

	/**
	 * 禁用管理员
	 * 
	 * @param adminid
	 * @return
	 */
	public Integer enableAdmin(int id, int isEnabele) {
		AdminMapper map = this.getMapper();
		Integer res = map.enableAdmin(id, isEnabele);
		return res;
	}
	
	/**
	 * 修改密码
	 * @param adminId
	 * @return
	 */
	public Integer changePassword(int adminId,String enPassword){
		AdminMapper map = this.getMapper();
		Integer res = map.changePassword(adminId,enPassword);
		return res;		
	}
}
